package cn.xopencode.recordservice.dal.mysql.mapper.sf6machine;

import cn.xopencode.common.framework.util.DateUtil;
import cn.xopencode.mybatis.core.query.QueryWrapperX;
import cn.xopencode.recordservice.dal.mysql.dataobject.kvoperation.KvOperationRecordOtherDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.sf6machine.Sf6MachineRecordOtherDO;
import cn.xopencode.recordservice.service.robotroom.bo.AdminRecordPageBO;
import cn.xopencode.recordservice.service.sf6machine.bo.Sf6MachineRecordOtherPageBO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface Sf6MachineRecordOtherMapper extends BaseMapper<Sf6MachineRecordOtherDO> {

    default IPage<Sf6MachineRecordOtherDO> selectPage(Sf6MachineRecordOtherPageBO pageBO) {
        if(pageBO.getInspectionTime() != null) {
            String inspectionTime = DateUtil.format(pageBO.getInspectionTime(), "yyyy-MM-dd");
            return selectPage(new Page<>(pageBO.getPageNo(), pageBO.getPageSize()),
                    new QueryWrapperX<Sf6MachineRecordOtherDO>()
                            .betweenIfPresent("inspection_time", inspectionTime + " 00:00:00", inspectionTime + " 23:59:59")
                            .orderByDesc("inspection_time"));
        }
        return selectPage(new Page<>(pageBO.getPageNo(), pageBO.getPageSize()),
                new QueryWrapperX<Sf6MachineRecordOtherDO>()
                        .orderByDesc("inspection_time"));
    }

    /**
     * 获取最新的一条记录
     * @return
     */
    default  Sf6MachineRecordOtherDO getLastRecord(){
        return selectOne(new QueryWrapperX<Sf6MachineRecordOtherDO>()
            .orderByDesc("inspection_time").last("limit 1"));
    }

    default Sf6MachineRecordOtherDO getSf6MachineRecordOtherDOByRecordCode(String recordCode){
        return selectOne(new QueryWrapper<Sf6MachineRecordOtherDO>().eq("record_code", recordCode).last(" limit 1"));
    }

    /**
     * 后台按条件分页查询列表数据
     * @param pageBO
     * @return
     */
    IPage<Sf6MachineRecordOtherDO> adminQuery(Page page, @Param("pageBO") AdminRecordPageBO pageBO);
}
